/**
 * @description users router
 */

const router = require('koa-router')()
const { userIdsSchema } = require('../validator/users')
const genValidator = require('../middleware/genValidator')
const { getUserList, updateIsFrozen, getCount, getMonthlyCount } = require('../controller/users')

// 路由前缀
router.prefix('/api/users')

// 获取用户列表
router.get('/', async ctx => {
  const { keyword = '', pageIndex, pageSize } = ctx.query
  const res = await getUserList(decodeURIComponent(keyword), pageIndex, pageSize)
  ctx.body = res
})

// 冻结用户
router.post('/froze', genValidator(userIdsSchema), async ctx => {
  const { ids } = ctx.request.body
  const res = await updateIsFrozen(ids, true)
  ctx.body = res
})

// 解除冻结
router.post('/unFroze', genValidator(userIdsSchema), async ctx => {
  const { ids } = ctx.request.body
  const res = await updateIsFrozen(ids, false)
  ctx.body = res
})

// 获取总数
router.get('/getCount', async ctx => {
  const res = await getCount()
  ctx.body = res
})

// 按月统计
router.get('/getMonthlyCount', async ctx => {
  const res = await getMonthlyCount()
  ctx.body = res
})

module.exports = router
